/*
 * etimer.h
 *
 *  Created on: 2021-12-09
 *      Author: chenss
 */

#ifndef ETIMER_H_
#define ETIMER_H_

#include "main.h"

#define USE_PROTOTHREAD 0

typedef uint64_t ETimer_Tick_t;

typedef struct  {
  ETimer_Tick_t start;
  ETimer_Tick_t interval;
}ETimer_t;

#if USE_PROTOTHREAD
#   include "protothread/pt.h"

#define PT_DELAY(tpt, ms) \
    do { \
        ETimer_Set(&(tpt)->timer, ms); \
        PT_WAIT_UNTIL(tpt, ETimer_Expired(&(tpt)->timer)); \
    } while(0)

struct tpt
{
    lc_t lc;
    ETimer_t timer;
};

#endif

void ETimer_Set(ETimer_t *t, uint32_t interval_ms);
void ETimer_Reset(ETimer_t *t);
void ETimer_Restart(ETimer_t *t);
int ETimer_Expired(ETimer_t *t);
ETimer_Tick_t ETimer_Remaining(ETimer_t *t);


#endif /* ETIMER_H_ */

/** @} */
/** @} */
